﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.DALFactory;

    public class InvSkuLocBLL : IBaseInfoDetail
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private static readonly IInvSkuLocDA invSkuLocDA = DataAccess.CreateInvSkuLocDA();
        private static readonly ILocationDA locationDA = DataAccess.CreateLocationDA();
        private PubParasInfo pubParasInfo;

        public InvSkuLocBLL()
        {
            this.pubParasInfo = null;
        }

        public InvSkuLocBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public InvSkuLocBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string DeleteDetail(List<string> mainID, List<string> subId, List<string> pk3)
        {
            string temp = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                int i;
                List<InvSkuLocInfo> invSkuLocInfos = new List<InvSkuLocInfo>();
                InvSkuLocInfo invSkuLocInfo = null;
                for (i = 0; i < mainID.Count; i++)
                {
                    invSkuLocInfo = invSkuLocDA.GetInvSkuLocByID(dataBase, tran, mainID[i], subId[i], pk3[i], this.dataBaseID[0]);
                    invSkuLocInfos.Add(invSkuLocInfo);
                }
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                if (invSkuLocDA.DeleteInvSkuLoc(dataBase, tran, mainID, subId, pk3, this.dataBaseID[0]) > 0)
                {
                    for (i = 0; mainID.Count > i; i++)
                    {
                        auditDataBLL.InsertAuditData<InvSkuLocInfo>(dataBase, tran, invSkuLocInfos[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "INV_SKU_LOC", this.dataBaseID[0]);
                        string CS00002 = temp;
                        temp = CS00002 + " 货主 " + mainID[i] + "货品 " + subId[i] + "货位" + pk3[i] + "; ";
                    }
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return temp;
        }

        public DataSet GetAllDetailInfo()
        {
            throw new NotImplementedException("bll层函数未实现");
        }

        public DataSet GetDetaiInfolByQueryEntity(object detailQuery)
        {
            throw new NotImplementedException("bll层函数未实现");
        }

        public object GetDetailInfoByID(string mainID, string subID, string pk3)
        {
            return invSkuLocDA.GetInvSkuLocByID(mainID, subID, pk3, this.dataBaseID[0]);
        }

        public List<object> GetDetailInfoListByID(string mainID, string subID)
        {
            throw new NotImplementedException("bll层函数未实现");
        }

        public string InsertDetail(object obj)
        {
            InvSkuLocInfo invSkuLocInfo = obj as InvSkuLocInfo;
            string invSkuLocID = string.Empty;
            try
            {
                if (invSkuLocInfo == null)
                {
                    return invSkuLocID;
                }
                invSkuLocInfo.UpdatedBy = invSkuLocInfo.CreatedBy;
                if (!locationDA.CheckLocationIDUnique(invSkuLocInfo.Loc, this.dataBaseID[0]))
                {
                    throw new Exception("货位 " + invSkuLocInfo.Loc + " 不存在,请重新输入!");
                }
                if (invSkuLocDA.CheckInvSkuLocIDUnique(invSkuLocInfo.OwnerID, invSkuLocInfo.SkuID, invSkuLocInfo.Loc, this.dataBaseID[0]))
                {
                    throw new Exception("货位 " + invSkuLocInfo.Loc + " 货主 " + invSkuLocInfo.OwnerID + " 货品 " + invSkuLocInfo.SkuID + " 已经存在,请重新输入!");
                }
                if (invSkuLocDA.InsertInvSkuLoc(invSkuLocInfo, this.dataBaseID[0]) > 0)
                {
                    new AuditDataBLL().InsertAuditData<InvSkuLocInfo>(null, invSkuLocInfo, AuditAction.Insert, this.pubParasInfo.UserID, "INV_SKU_LOC", this.dataBaseID[0]);
                    invSkuLocID = invSkuLocInfo.Loc;
                }
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return invSkuLocID;
        }

        public string UpdateDetail(object obj)
        {
            string invSkuLocID = string.Empty;
            try
            {
                InvSkuLocInfo invSkuLocInfo = obj as InvSkuLocInfo;
                InvSkuLocInfo oldInvSkuLocInfo = invSkuLocDA.GetInvSkuLocByID(invSkuLocInfo.OwnerID, invSkuLocInfo.SkuID, invSkuLocInfo.Loc, this.dataBaseID[0]);
                if (invSkuLocDA.UpdateInvSkuLoc(invSkuLocInfo, this.dataBaseID[0]) > 0)
                {
                    new AuditDataBLL().InsertAuditData<InvSkuLocInfo>(oldInvSkuLocInfo, invSkuLocInfo, AuditAction.Update, this.pubParasInfo.UserID, "INV_SKU_LOC", this.dataBaseID[0]);
                    invSkuLocID = invSkuLocInfo.Loc;
                }
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return invSkuLocID;
        }

        public string UpdateIsActive(List<string> mainID, List<string> subId, string isActive)
        {
            throw new NotImplementedException();
        }
    }
}

